package cz.via.facepalm.calendar;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cz.via.facepalm.shared.CActivity;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

public class CalendarExpandableList extends ExpandableListActivity 
{
	CActivity [] activities;
	
	
	public void onCreate(Bundle savedInstaceState)
	{	
		super.onCreate(savedInstaceState);
		setContentView(R.layout.calendarexpladablelist);
		/*
		//Dummy data		
		activities = new CActivity[1];
		CActivity a1 = new CActivity();
		a1.setName("Via deadline");
		a1.setWeeklyTime("1.12.2010,7:30");
		a1.setPlace("KN:E-301");
		a1.setType("seminar");
		a1.setProject("VIA project");
		activities[0] = a1;
		
		try
		{					
			SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
				this,
				createGroupList(),              
				R.layout.group_row,             
				new String[] { "Hour", "Date", "Title" },  
				new int[] { R.id.row_hour,R.id.row_date,R.id.row_title },
				createChildList(),              
				R.layout.child_row,             
				new String[] {"Subname","Subvalue"},      
				new int[] { R.id.grp_name, R.id.grp_value}     
			);			
			setListAdapter( expListAdapter );
		}
		catch(Exception e)
		{			
			System.out.println("Errr +++ "+ e.getMessage());
		}		
		
		Button prevBtn = (Button) findViewById(R.id.prev_btn);
		prevBtn.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) 
			{
				System.out.println("Prev clicked");
			}
		}
		);
		
		Button nextBtn = (Button) findViewById(R.id.next_btn);
		nextBtn.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) 
			{
				System.out.println("Next clicked");
			}
		}
		);
		*/
	}
		
	private List<HashMap<String,String>> createGroupList() 
	{
		ArrayList<HashMap<String,String>> result = new ArrayList<HashMap<String,String>>();
		for( int i = 0 ; i < activities.length ; ++i ) 
		{ 		
			String[] splited = activities[i].getWeeklyTime().split(",");			
			HashMap<String, String> m = new HashMap<String, String>();			
			m.put( "Date",splited[0]);
			m.put( "Hour",splited[1]);
			m.put( "Title",activities[i].getName());
			result.add( m );
		}
		return (List<HashMap<String,String>>)result;
   }
   
   private List<ArrayList<HashMap<String,String>>> createChildList() {
	   
     ArrayList<ArrayList<HashMap<String,String>>> result = new ArrayList<ArrayList<HashMap<String,String>>>();
     for( int i = 0 ; i < activities.length ; ++i ) 
     {         
  	   ArrayList<HashMap<String,String>> secList = new ArrayList<HashMap<String,String>>();
  	       	       	  
//	   HashMap<String, String> child1 = new HashMap<String, String>();    		   
//	   child1.put( "Subname", "Project:"  );
//	   child1.put( "Subvalue", activities[i].getProject() );    		   
//	   secList.add( child1 );
//	   
//	   HashMap<String, String> child2 = new HashMap<String, String>();    		   
//	   child2.put( "Subname", "Place:"  );
//	   child2.put( "Subvalue", activities[i].getPlace());    		   
//	   secList.add( child2 );
//	   
//	   HashMap<String, String> child3 = new HashMap<String, String>();    		   
//	   child3.put( "Subname", "Type:"  );
//	   child3.put( "Subvalue", activities[i].getType() );    		   
//	   secList.add( child3 );		 	  
		   
  	   result.add( secList );
     }
     return (List<ArrayList<HashMap<String,String>>>)result;	   
   }
   
   public void  onContentChanged  () 
   {       
       super.onContentChanged();
       System.out.println("onContentChanged");
   }
      
   public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
       System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);
       return true;
   }
	
	public void  onGroupExpand  (int groupPosition) 
	{
		try
		{
			System.out.println("Group exapanding Listener => groupPosition = " + groupPosition);
		}
		catch(Exception e)
		{
			System.out.println(" groupPosition Errrr +++ " + e.getMessage());
		}
	}
}
